package cn.gxa.edu_adm.servlet;


import cn.gxa.edu_adm.commn.model.ResultData;
import cn.gxa.edu_adm.service.impl.AdminServiceImpl;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;


@WebServlet(name = "checkLogin",urlPatterns = "/checkLogin")
public class CheckLoginServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置编码格式
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        request.getRequestURL();

        String username=request.getParameter("username");
        String password=request.getParameter("password");
        //数据库验证
        ResultData resultData=new AdminServiceImpl().checkLogin(username,password);
        //数据保存在session中  以方便记录这个用户已经登陆过了
        request.getSession().setAttribute("admin",resultData.getData());
        //map响应给前端
        PrintWriter writer = response.getWriter();
        writer.print(JSON.toJSONString(resultData));
        writer.close();


    }


}
